package org.example.mrrope.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.example.mrrope.service.OwnDoKnotWorkOrderService;
import org.example.mrrope.service.OwnSewingWorkOrderService;
import org.example.mrrope.utils.DingUtil;
import org.example.mrrope.utils.FormUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.resource.AbstractResourceResolver;

import java.util.ArrayList;
import java.util.List;

/**
 * 缝纫 个人派工单
 */
@Service
@Slf4j
public class OwnSewingWorkOrderServiceImpl implements OwnSewingWorkOrderService {

    private static JSONObject ownJsonB;
    @Autowired
    private DingUtil dingUtil;

    @Override
    @Async
    public Integer saveOwnSewingWorkOrder(String id) {
        try {
            //得到缝纫车间派工单数据
            log.info("开始调用宜搭查询接口");
            JSONObject jsonObject = new JSONObject(dingUtil.getYidaById(id));
            log.info("调用查询接口返回缝纫车间派工单数据" + jsonObject);
            // body
            int res = 1;
            if (jsonObject.has("tableField_lxifktcx")) {
                JSONArray OwnerTable = jsonObject.getJSONArray("tableField_lxifktcx");
                for (int i = 0; i < OwnerTable.length(); i++) {
                    JSONObject ownJson = getUpJson(jsonObject,id);
                    ownJson.put("employeeField_m2alrm9g", OwnerTable.getJSONObject(i).has("employeeField_lxifktcy_id")?OwnerTable.getJSONObject(i).get("employeeField_lxifktcy_id"):"");//作业人
                    if (OwnerTable.getJSONObject(i).has("numberField_lxifktcz")){
                        int numberFiz = OwnerTable.getJSONObject(i).optInt("numberField_lxifktcz");
                        ownJson.put("numberField_lugarhfl", numberFiz);//作业数量
                        ownJson.put("numberField_m2amdqvl", numberFiz);//未上交数
                    }
                    ownJson.put("dateField_m2alrm9i",  OwnerTable.getJSONObject(i).has("dateField_lxifktd0")?OwnerTable.getJSONObject(i).get("dateField_lxifktd0"):"");//交付日期
                    ownJson.put("textField_m2alrm9n", "已下发待确认");
                    log.info("*************新建缝纫车间个人派工单请求参数---->>>>" + ownJson);
                    //郭扬晨
                    String result = dingUtil.startInstance(ownJson, "314764473436461353", "FORM-E1591C232E59400DAF7011CD7F2D1EF4R7NK");//李浩
                    log.info("*************新建缝纫车间个人派工单返回参数---->>>>" + result);
                    if (result != null) {
                        log.info("新建缝纫车间个人派工单成功，单号====》" + result);
                        res = 0;
                    } else {
                        log.info("新建缝纫车间个人派工单失败");
                        res = 1;
                    }
                }
//                String upRes = dingUtil.UpdateYiDa(id, jsonObject);
//                log.info("生成缝纫个人任务后更新结果===》" + upRes + jsonObject);
                return res;
            } else {
                log.info("未找到工人");
                return 1;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private static JSONObject getUpJson(JSONObject jsonObject, String id) {
        JSONObject ownJson = new JSONObject();
        JSONArray jsonArrayOne = FormUtils.getAssociationFormField("FORM-A6E69C4290D3475C8F31A1DF76A03201NPH8", jsonObject.get("textField_lugarhfe").toString(), id);
        ownJson.put("associationFormField_lxtusqg0", jsonArrayOne);
        ownJson.put("numberField_m2amdqvk", 0);//订单总量
        ownJson.put("textField_lx1mddjk", jsonObject.get("textField_lx1mddjk"));//客户名称
        ownJson.put("textField_lugarhfd", jsonObject.has("textField_lugarhfd")?jsonObject.get("textField_lugarhfd"):"");//半成品B号
        ownJson.put("textField_lugarhfe", jsonObject.get("textField_lugarhfe"));//客人货号
        ownJson.put("imageField_lugarhfm", jsonObject.has("imageField_lugarhfm")?jsonObject.get("imageField_lugarhfm"):"");//成品图片
        ownJson.put("textField_lxifktd1", jsonObject.get("textField_lxifktd1"));//产品名称
        ownJson.put("textField_lugarhfx", jsonObject.get("textField_lugarhfx"));//规格重量
        ownJson.put("textField_lugarhg6", jsonObject.optInt("textField_lugarhg6"));//颜色
        ownJson.put("textField_lxcvvpdu", jsonObject.optInt("textField_lxcvvpdu"));//配件
        ownJson.put("textareaField_lvxftd6b", jsonObject.has("textareaField_lvxftd6b")?jsonObject.get("textareaField_lvxftd6b"):"");//包装方式
        ownJson.put("textField_m2an4zw2", jsonObject.has("textField_lyz6ump9")?jsonObject.get("textField_lyz6ump9"):"");//SO号
        ownJson.put("textField_lugarhga", jsonObject.has("textField_lugarhga")?jsonObject.get("textField_lugarhga"):"");//工价
        ownJson.put("textField_lugarhgb", jsonObject.has("textField_lugarhgb")?jsonObject.get("textField_lugarhgb"):"");//注意点
        //绳子
        if (jsonObject.has("tableField_lxwy3kxi")){
            List<JSONObject> listJOSNObject = new ArrayList<>();
            JSONArray jsonArrayTow = jsonObject.getJSONArray("tableField_lxwy3kxi");
            for (int i = 0; i < jsonArrayTow.length(); i++) {
                JSONObject ownJsonTow = new JSONObject();
                JSONObject ownJsonB = jsonArrayTow.getJSONObject(i);
                ownJsonTow.put("textField_lujgt57l",ownJsonB.get("textField_lxxbi9ma"));//半成品绳子C号
                ownJsonTow.put("textField_lxcy3v0m",ownJsonB.get("textField_lxxbi9mb"));//绳子名称
                ownJsonTow.put("numberField_lujhx4e0",ownJsonB.get("numberField_lxxbi9mc"));//绳子口径
                ownJsonTow.put("textField_lxd537sl",ownJsonB.get("textField_lxxbi9md"));//绳子规格
                ownJsonTow.put("textareaField_lvxhxh0a",ownJsonB.has("textareaField_lxxbi9mf")?ownJsonB.get("textareaField_lxxbi9mf"):"");//花色挂法
                listJOSNObject.add(ownJsonTow);
            }
            ownJson.put("tableField_lujgt57k",listJOSNObject);
        }
        //配件子表
        if (jsonObject.has("tableField_lvxe56yi")){
            List<JSONObject> listJOSNObject = new ArrayList<>();
            JSONArray jsonArrayTow = jsonObject.getJSONArray("tableField_lvxe56yi");
            for (int j = 0; j < jsonArrayTow.length(); j++) {
                JSONObject ownJsonTow = new JSONObject();
                JSONObject ownJsonB = jsonArrayTow.getJSONObject(j);
                ownJsonTow.put("textField_lxcvvpdl", ownJsonB.get("textField_lx07bljk"));//半成品B号
                ownJsonTow.put("textField_lxcvvpdm", ownJsonB.get("textField_lujgt59d"));//配件名称
                ownJsonTow.put("textField_lxcvvpdn", ownJsonB.get("textField_lujgt59f"));//配件编号
                ownJsonTow.put("textField_lxcvvpdo", ownJsonB.get("textField_lx3ck53r"));//规格描述
                ownJsonTow.put("textField_lxcvvpdp", ownJsonB.has("textField_lx2luc3t")?ownJsonB.get("textField_lx2luc3t"):"");//是否打孔
                ownJsonTow.put("textField_lxcvvpdq", ownJsonB.has("textField_lx2luc3s")?ownJsonB.get("textField_lx2luc3s"):"");//打孔规格
                ownJsonTow.put("textField_lxcvvpdr", ownJsonB.has("textField_lwg902pd")?ownJsonB.get("textField_lwg902pd"):"");//供应商
                ownJsonTow.put("imageField_lxcvvpds", ownJsonB.has("imageField_lwg902pf")?ownJsonB.get("imageField_lwg902pf"):"");//例图
                ownJsonTow.put("numberField_lxwv7czp", ownJsonB.has("numberField_lxwv7czp")?ownJsonB.get("numberField_lxwv7czp"):"");//总领量
                ownJsonTow.put("numberField_lxvgkgg8", ownJsonB.has("numberField_lxvgkgg8")?ownJsonB.get("numberField_lxvgkgg8"):"");//标准用量
                ownJsonTow.put("textField_lxcvvpdt", ownJsonB.has("textField_lwee5prf")?ownJsonB.get("textField_lwee5prf"):"");//备注
                listJOSNObject.add(ownJsonTow);
            }
            ownJson.put("tableField_lxcvv39j",listJOSNObject);
        }
        return ownJson;
    }
}
